package com.ibeifeng.crm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibeifeng.crm.bean.ClassInfo;
import com.ibeifeng.crm.service.ClassInfoService;


@Controller
@RequestMapping("/pages/classinfo/")
public class ClassInfoController {

	private Logger log = Logger.getLogger(ClassInfoController.class);
	@Resource
	ClassInfoService classInfoService;

	@RequestMapping(value = "query", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		String pg = request.getParameter("page");
		String pgSize = request.getParameter("rows");
		String column = request.getParameter("column");
		String keyWord = request.getParameter("keyWord");
		// 若要显示的第几页为空，那么默认为1
		if (null == pg || "".equals(pg)) {
			pg = "1";
		}
		// 显示多少行为空，那么默认为10
		if (null == pgSize || "".equals(pgSize)) {
			pgSize = "10";
		}

		// 将pg,pgSize转化为int型的数据
		int page = Integer.parseInt(pg);
		int pageSize = Integer.parseInt(pgSize);
		// 将从前台获得的分页参数放到findAllSplit查询分页的方法中

		List<ClassInfo> classInfos = classInfoService.findAllSplit(page, pageSize,
				column, keyWord);
		int count = classInfoService.count();
		// 将从后台获得的course对象列表，以及总共查询多少个符合条件下的记录个数，以Map格式传到前台
		Map map = new HashMap();
		map.put("total", count);
		map.put("rows", classInfos);
		JSONArray obj = JSONArray.fromObject(map);
		String str = obj.toString();
		String js = str.substring(1, str.length() - 1);
		return js;
	}

	//获取全部的classinfo信息
	@RequestMapping(value = "queryMethod", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String queryMethod() {
		List<ClassInfo> classInfos = classInfoService.queryMethod();
		JSONArray obj = JSONArray.fromObject(classInfos);
		String str = obj.toString();
		return str;
	}
	
	//获取一个指定的classinfo信息
	@RequestMapping(value = "queryOne", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String queryOne(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("id"));
		ClassInfo classInfo = classInfoService.queryOne(id);
		JSONArray obj = JSONArray.fromObject(classInfo);
		String str = obj.toString();
		String js = str.substring(1, str.length()-1);
		return js;
	}
	
	
	@RequestMapping(value = "add")
	@ResponseBody
	public String add(ClassInfo classInfo) {
		// sql执行情况，若大于0，那么则执行成功
		int result = classInfoService.add(classInfo);
		// 若执行成功，则返回前台1,否则则返回0
		Map map = new HashMap();
		if (result > 0) {
			map.put("res", "true");
		} else {
			map.put("res", "false");
		}
		// 将执行结果存放在map中，以json格式返回前台
		JSONObject js = JSONObject.fromObject(map);
		return js.toString();
	}

	@RequestMapping(value = "update")
	@ResponseBody
	public String update(ClassInfo classInfo) {
		// sql执行情况，若大于0，那么则执行成功
		int result = classInfoService.update(classInfo);
		// 若执行成功，则返回前台1,否则则返回0
		Map map = new HashMap();
		if (result > 0) {
			map.put("res", "true");
		} else {
			map.put("res", "false");
		}
		JSONObject js = JSONObject.fromObject(map);
		return js.toString();
	}

	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(String ids, HttpServletRequest request) {
		// 获取需要删除的ids数据,将转换为int数组
		String[] split = ids.split(",");
		int [] idsdel =  new int [split.length]  ;
		for (int i = 0; i < split.length; i++) {
			idsdel[i] = Integer.parseInt(split[i]);
		}
		// sql执行情况，若大于0，那么则执行成功
		int result = classInfoService.delete(idsdel);
		// 若执行成功，则返回前台1,否则则返回0
		Map map = new HashMap();
		if (result > 0) {
			map.put("res", "true");
		} else {
			map.put("res", "false");
		}
		JSONObject js = JSONObject.fromObject(map);
		return js.toString();
	}
}
